//-------------------------------------------------------------------
//	Copyright (c) 2010-2012 Zhirnov Andrey
//	This file is part of the "UXGEN" project.
//	See copyright notice in "Engine.h".
//-------------------------------------------------------------------

#ifndef _UX_FILE_BUFFER_H
#define _UX_FILE_BUFFER_H

#include "IFile.h"
#include "libarchive/lib_archive.h"

namespace Engine
{
namespace FileSystem
{

	//
	// File Buffer
	//

	class _EXPORT_ CFileBuf : public IFile
	{
	protected:
		string				_sName;
		binary_buffer_t		_aData;
		usize				_uPos;
		e_rw_mode::type		_eRWmode;
		const e_file::type	_eFileType;
		
	protected:
		CFileBuf();

	public:
		CFileBuf(e_file::type eType);
		CFileBuf(const char *pFileName, e_file_open_flags::type eFlags);
		virtual ~CFileBuf();
		
		void Clear();

		bool Save(const char *pFileName = nullptr);
		bool Save(IFilePtr pFile);

		bool Load(const char *pFileName = nullptr);
		bool Load(IFilePtr pFile);

		// Archive files //
		bool LoadZip(const string &sArchiveName, const string &sFileName, const char *pPassword = nullptr);

#	ifndef UX_LIBARCHIVE_DISABLE
		bool LoadArchive(const string &sArchiveName, const string &sFileName);
		bool SaveArchive(const string &sArchiveName, const string &sFileName, e_la_compression::type eComp, e_la_format::type eFormat);
#	endif


		// IFile //
		virtual bool			IsOpen()	const	{ return true; }
		virtual const string &	GetName()	const	{ return _sName; }
		virtual e_rw_mode::type	GetMode()	const	{ return e_rw_mode::READ_WRITE; }

		virtual uint Read(void *pBuf, uint uSize);
		virtual uint Write(const void *pBuf, uint uSize);
		virtual uint Seek(uint uOffset, e_seek::type eSeekFlag);

		virtual uint GetSize()	const;
		virtual uint GetPos()	const;

		virtual uint64 SeekL(uint64 uOffset, e_seek::type eSeekFlag = e_seek::CURRENT);
		virtual uint64 GetSizeL()	const;
		virtual uint64 GetPosL()	const;

		virtual e_file::type GetFileType()	const	{ return _eFileType; }

		// CEngBaseObject //
		virtual void Release();
	};

//-------------------------------------------------------------------

}	// FileSystem
}	// Engine

#endif	// _UX_FILE_BUFFER_H